// 1.Static Batching静态合批
// 原理：空间或时间
// 多次绘一个物体 变成 一次绘制多个物体
// 要有meshFilter和meshRenderer

//自动添加组件
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]

public class MeshCombiner : MonoBehaviour
{

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.J))
        {
            CombineMeshes()
        }
    }
    // Start is called before the first frame update
    void CombineMeshes()
    {
        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];
        int i = 0;
        while (i < meshFilters.Length)
        {
            combine[i].mesh = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            meshFilters[i].gameObject.SetActive(false);
            i++;
        }
        transform.GetComponent<MeshFilter>().mesh = new Mesh();
        transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
        gameObject.SetActive(true);
    }
}